using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Media;
using Common;
using Common.NotifyEvent;

namespace SProject.BasicInformation
{
	[Serializable]
	public class TargetItem : NotifyPropertyChanged
	{
		private int _TubeNo;

		private int _ChannelNo;

		private string _Name;

		private string _Dye;

		private string _TubeName;

		private Color _Color = Colors.Black;

		public int TubeNo
		{
			get
			{
				return _TubeNo;
			}
			set
			{
				_TubeNo = value;
				OnPropertyChanged("TubeNo");
			}
		}

		public int ChannelNo
		{
			get
			{
				return _ChannelNo;
			}
			set
			{
				_ChannelNo = value;
				OnPropertyChanged("ChannelNo");
			}
		}

		public string Name
		{
			get
			{
				return _Name;
			}
			set
			{
				_Name = value;
				OnPropertyChanged("Name");
			}
		}

		public string Dye
		{
			get
			{
				return _Dye;
			}
			set
			{
				_Dye = value;
				OnPropertyChanged("Dye");
			}
		}

		public string TubeName
		{
			get
			{
				return _TubeName;
			}
			set
			{
				_TubeName = value;
				OnPropertyChanged("TubeName");
			}
		}

		public Color Color
		{
			get
			{
				return _Color;
			}
			set
			{
				if (!(_Color == value))
				{
					_Color = value;
					OnPropertyChanged("Color");
				}
			}
		}

		public TargetItem()
		{
		}

		public TargetItem(TargetItem item)
		{
			Copy(item);
		}

		public void Copy(TargetItem item)
		{
			_TubeNo = item._TubeNo;
			_ChannelNo = item._ChannelNo;
			_Name = item._Name;
			_Dye = item._Dye;
			_TubeName = item._TubeName;
			_Color = item.Color;
		}

		public bool Equals(TargetItem item)
		{
			if (item == null)
			{
				return false;
			}
			if (base.Equals(item))
			{
				return true;
			}
			if (TubeNo == item.TubeNo && ChannelNo == item.ChannelNo && Name == item.Name && Dye == item.Dye && TubeName == item.TubeName)
			{
				return Color == item.Color;
			}
			return false;
		}

		public void SaveToStream(StreamEx stream)
		{
			stream.Write(PropertyKey.Key_ProjectTargetItem);
			List<PropertyKey> list = new List<PropertyKey>();
			List<byte[]> list2 = new List<byte[]>();
			list.Add(PropertyKey.Key_TubeNo);
			list2.Add(BytesConverter.GetBytes(TubeNo));
			list.Add(PropertyKey.Key_ChannelNo);
			list2.Add(BytesConverter.GetBytes(ChannelNo));
			if (!string.IsNullOrEmpty(TubeName))
			{
				list.Add(PropertyKey.Key_TubeName);
				list2.Add(BytesConverter.GetBytes(TubeName));
			}
			if (!string.IsNullOrEmpty(Dye))
			{
				list.Add(PropertyKey.Key_Dye);
				list2.Add(BytesConverter.GetBytes(Dye));
			}
			if (!string.IsNullOrEmpty(Name))
			{
				list.Add(PropertyKey.Key_TargetName);
				list2.Add(BytesConverter.GetBytes(Name));
			}
			list.Add(PropertyKey.Key_Color);
			list2.Add(BytesConverter.GetBytes(Color.ToString()));
			stream.Write(PropertyKey.Key_Index);
			stream.WriteIndex(list, list2);
			stream.Flush();
		}

		public bool ReadFromStream(StreamEx stream)
		{
			PropertyKey propertyKey = stream.ReadKey();
			if (propertyKey == PropertyKey.Key_ProjectTargetItem)
			{
				propertyKey = stream.ReadKey();
				if (propertyKey == PropertyKey.Key_Index)
				{
					List<PropertyKey> keys;
					List<byte[]> dates;
					stream.ReadIndex(out keys, out dates);
					for (int i = 0; i < keys.Count; i++)
					{
						switch (keys[i])
						{
						case PropertyKey.Key_TubeNo:
							TubeNo = BytesConverter.BytesToInt(dates[i]);
							break;
						case PropertyKey.Key_ChannelNo:
							ChannelNo = BytesConverter.BytesToInt(dates[i]);
							break;
						case PropertyKey.Key_TubeName:
							TubeName = BytesConverter.BytesToString(dates[i]);
							break;
						case PropertyKey.Key_Dye:
							Dye = BytesConverter.BytesToString(dates[i]);
							break;
						case PropertyKey.Key_TargetName:
							Name = BytesConverter.BytesToString(dates[i]);
							break;
						case PropertyKey.Key_Color:
							Color = (Color)ColorConverter.ConvertFromString(BytesConverter.BytesToString(dates[i]));
							break;
						}
					}
					return true;
				}
				stream.Seek(-4L, SeekOrigin.Current);
				while (true)
				{
					switch (stream.ReadKey())
					{
					case PropertyKey.Key_TubeNo:
						TubeNo = stream.ReadInt();
						break;
					case PropertyKey.Key_ChannelNo:
						ChannelNo = stream.ReadInt();
						break;
					case PropertyKey.Key_TubeName:
						TubeName = stream.ReadString();
						break;
					case PropertyKey.Key_Dye:
						Dye = stream.ReadString();
						break;
					case PropertyKey.Key_TargetName:
						Name = stream.ReadString();
						break;
					default:
						stream.Seek(-4L, SeekOrigin.Current);
						return true;
					}
				}
			}
			stream.Seek(-4L, SeekOrigin.Current);
			return false;
		}
	}
}
